﻿using System;
using System.Data;

namespace NetBay.Core.Extensions
{
    public static class DbDataReaderEx
    {

        /// <summary>
        /// Gets the value.
        /// </summary>
        /// <param name="db">The db.</param>
        /// <param name="columnName">Name of the column.</param>
        /// <returns></returns>
        public static object GetValue(this IDataReader db, string columnName)
        {
            if (db[columnName] == DBNull.Value)
                return null;
            else
                return db[columnName];
        }

        /// <summary>
        /// Gets the string value.
        /// </summary>
        /// <param name="db">The db.</param>
        /// <param name="columnName">Name of the column.</param>
        /// <param name="defaultReturnValue">The default return value.</param>
        /// <returns></returns>
        public static string GetStringValue(this IDataReader db, string columnName, string defaultReturnValue = "")
        {
            if (db[columnName] != DBNull.Value && db[columnName] != null && !string.IsNullOrEmpty(db[columnName].ToString()))
                return db[columnName].ToString();
            else
                return defaultReturnValue;
        }

        /// <summary>
        /// Gets the long value.
        /// </summary>
        /// <param name="db">The db.</param>
        /// <param name="columnName">Name of the column.</param>
        /// <param name="defaultReturnValue">The default return value.</param>
        /// <returns></returns>
        public static long GetLongValue(this IDataReader db, string columnName, long defaultReturnValue = 0)
        {
            try
            {
                if (db[columnName] != DBNull.Value && db[columnName] != null && !string.IsNullOrEmpty(db[columnName].ToString()))
                    return Convert.ToInt64(db[columnName].ToString());
                else
                    return defaultReturnValue;
            }
            catch
            {
                return defaultReturnValue;
            }
        }

        /// <summary>
        /// Gets the int value.
        /// </summary>
        /// <param name="db">The db.</param>
        /// <param name="columnName">Name of the column.</param>
        /// <param name="defaultReturnValue">The default return value.</param>
        /// <returns></returns>
        public static int GetIntValue(this IDataReader db, string columnName, int defaultReturnValue = 0)
        {
            try
            {
                if (db[columnName] != DBNull.Value && db[columnName] != null && !string.IsNullOrEmpty(db[columnName].ToString()))
                    return Convert.ToInt32(db[columnName].ToString());
                else
                    return defaultReturnValue;
            }
            catch
            {
                return defaultReturnValue;
            }
        }

        /// <summary>
        /// Gets the boolean value.
        /// </summary>
        /// <param name="db">The db.</param>
        /// <param name="columnName">Name of the column.</param>
        /// <param name="defaultReturnValue">The default return value.</param>
        /// <returns></returns>
        public static bool GetBooleanValue(this IDataReader db, string columnName, bool defaultReturnValue = false)
        {
            try
            {
                if (db[columnName] != DBNull.Value && db[columnName] != null && !string.IsNullOrEmpty(db[columnName].ToString()))
                    return Convert.ToBoolean(int.Parse(db[columnName].ToString()));
                else
                    return defaultReturnValue;
            }
            catch
            {
                return defaultReturnValue;
            }
        }

        /// <summary>
        /// Convert an object to its equivalent DateTime. If null then DateTime.DateMin
        /// </summary>
        /// <param name="db">The db.</param>
        /// <param name="columnName">Name of the column.</param>
        /// <returns>DateTime equivalent</returns>
        public static DateTime GetDateTimeValue(this IDataReader db, string columnName)
        {
            try
            {
                DateTime l_oDt;
                if (db[columnName] != DBNull.Value && db[columnName] != null && !string.IsNullOrEmpty(db[columnName].ToString()))
                    return DateTime.TryParse(db[columnName].ToString(), out l_oDt) ? l_oDt : default(DateTime);
                else
                    return default(DateTime);
            }
            catch
            {
                return default(DateTime);
            }
        }

        /// <summary>
        /// Gets the enum value.
        /// </summary>
        /// <param name="db">The db.</param>
        /// <param name="columnName">Name of the column.</param>
        /// <returns></returns>
        public static T GetEnumValue<T>(this IDataReader db, string columnName)
        {
            if (db[columnName] == DBNull.Value || db[columnName] == null)
                return default(T);
            else
                return (T)Enum.Parse(typeof(T), db[columnName].ToString());
        }

    }
}
